package com.javabaas.server.novel_server.book.controller;

import com.javabaas.server.novel_server.annotate.SerializedField;
import com.javabaas.server.novel_server.book.service.BookService;
import com.javabaas.server.novel_server.common.Constant;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/book")
public class BookController {



    @Autowired
    BookService bookService;

    private Logger logger = Logger.getLogger(BookController.class);


    /**
     * 获取某个rank下的书籍列表
     *
     * @param rankId 排行榜id
     *
     * type: 1 rank/ 2 monthRank/ 3 totalRank
     */
    @RequestMapping(value = "/rank/{rankId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取排行榜下的书籍列表", notes = "根据rankId进行查询书籍")
    @ApiImplicitParam(name = "rankId", value = "排行榜Id", required = true, dataType = "String", paramType = "path")
    public Object getBooksWithRankId(HttpServletRequest request, @PathVariable String rankId) {
        return bookService.getBookList(request, rankId);
    }


    /**
     *搜索书籍列表
     *
     * 参数type: 1 title, 2 tag, 3 auth
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiOperation(value = "搜索书籍", notes = "根据标题／标签／作者进行模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "s", value = "搜索的内容", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1 title, 2 tag, 3 auth", required = true, dataType = "String", paramType = "query")
    })
    public Object searchBooks(HttpServletRequest request) {
        return bookService.searchBookList(request);
    }


    /**
     * 搜索分类数据列表
     *
     */

    @ApiOperation(value = "按照分类搜索书籍", notes = "根据分类进行模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "major", value = "搜索的分类名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别: male/female", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "hot,new,over,reputation", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/by-categories", method = RequestMethod.GET)
    public Object categories(HttpServletRequest request) {
        return bookService.categories(request);
    }

    /**
     * 获取推荐的数据
     *
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.GET)
    @ApiOperation(value = "查询某本书籍的推荐书籍", notes = "传入书籍Id，进行查询")
    @ApiImplicitParam(name = "bookId", value = "书籍Id", required = true, dataType = "String", paramType = "query")
    public Object getRecommendBooks(HttpServletRequest request) {
        return bookService.recommendBooks(request);
    }

    /**
     * 获取书籍的某章节内容
     */
    @RequestMapping(value = "/{bookId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取书籍的某章节内容", notes = "传入书籍Id&index进行查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bookId", value = "书籍ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "index", value = "第几章节", required = true, dataType = "int", paramType = "query")
    })
    public Object getBookBody(HttpServletRequest request, @PathVariable String bookId) {
        Integer index = 1;
        String indexStr = request.getParameter("index");
        if (!StringUtils.isEmpty(indexStr)) {
            index = Integer.parseInt(indexStr);
        }
        return bookService.body(bookId, index);
    }

    @SerializedField(includes = {"_id", "title", "cover", "updated", "author", "lastChapter", "type"})
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    @ApiOperation(value = "更新书籍的最新章节信息", notes = "传入书籍Id列表")
    @ApiImplicitParam(name = "books", value = "书籍列表", required = true, dataType = "String", paramType = "query")
    public Object getBookUpdate(HttpServletRequest request) {
        return bookService.update(request);
    }


    /**
     *下载书籍内容
     */
    @RequestMapping(value = "dw/{bookId}", method = RequestMethod.GET)
    @ApiOperation(value = "下载书籍，每次下载10章", notes = "传入书籍Id")
    @ApiImplicitParam(name = "bookId", value = "书籍id", required = true, dataType = "String", paramType = "path", example = "59b499177118e6562810f2ca")
    public Object downloadBook(HttpServletRequest request, @PathVariable String bookId) {
        Integer page = 0;
        Integer pagesize = Constant.PAGE_DOWNLOAD_SIZE;
        String pageStr = request.getParameter("page");
        String pagesizeStr = request.getParameter("pageSize");
        if (!StringUtils.isEmpty(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (!StringUtils.isEmpty(pagesizeStr)) {
            pagesize = Integer.parseInt(pagesizeStr);
        }
        return bookService.downloadBookBodys(bookId, page, pagesize);
    }
}
